package com.luozongyao.msmService.controller;

import com.luozongyao.commonutils.R;
import com.luozongyao.msmService.service.MsmApiService;
import com.luozongyao.msmService.utils.RandomUtil;
import com.sun.xml.internal.bind.v2.TODO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @program: guli_parent
 * @description:
 * @author: Zongyao Luo
 * @create: 2021-08-19 23:20
 **/
@RestController
@CrossOrigin
@RequestMapping("/eduMsm/msm")
public class MsmApiController {

    @Autowired
    private MsmApiService msmApiService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;


    @Autowired
    JavaMailSenderImpl javaMailSender;

    @GetMapping(value = "/send/{phone}")
    public R code(@PathVariable String phone) {
        String code = redisTemplate.opsForValue().get(phone);
        if (!StringUtils.isEmpty(code)){ return R.ok();}
        code = RandomUtil.getFourBitRandom();

        Map<String,Object> param = new HashMap<>();
        param.put("code",code);
        boolean isSend = msmApiService.send(phone,"SMS_180051135",param);
        if (isSend){
            redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);
            return R.ok();
        }else {
            return R.error().message("发送短信失败");
        }

    }


    @GetMapping(value = "/sendEmail/{email}")
    public R emailCode(@PathVariable String email) {
//        SimpleMailMessage mailMessage = new SimpleMailMessage();
//        mailMessage.setSubject("测试学习");
//        String code = redisTemplate.opsForValue().get(qq);
//        if (!StringUtils.isEmpty(qq)) code = RandomUtil.getFourBitRandom();
//
//        Map<String,Object> param = new HashMap<>();
//        param.put("emailCode",code);
//        redisTemplate.opsForValue().set(qq,code,5, TimeUnit.MINUTES);
//        mailMessage.setText("您的验证码为"+code);
//        mailMessage.setTo(qq+"@qq.com");
//        mailMessage.setFrom("1330074712@qq.com");
//        javaMailSender.send(mailMessage);
//        return R.ok();

        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setSubject("用户注册验证");
        String code = redisTemplate.opsForValue().get(email);
        if (!StringUtils.isEmpty(email)) {
            code = RandomUtil.getFourBitRandom();
        }

        Map<String,Object> param = new HashMap<>();
        param.put("emailCode",code);
        redisTemplate.opsForValue().set(email,code,5, TimeUnit.MINUTES);
        mailMessage.setText("您的验证码为:"+code+",5分钟内有效");
        mailMessage.setTo(email);
        mailMessage.setFrom("1330074712@qq.com");
        javaMailSender.send(mailMessage);
        return R.ok();
    }
}
